package com.mimosa.common.web;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;

import com.mimosa.util.config.Config;

/**
 * 将系统中的配置常量放到Servlet的ApplicationContext中
 * 
 * @author ggeagle
 * 
 */
@SuppressWarnings("serial")
public class InitConfigServlet extends HttpServlet {

	final Logger log = org.slf4j.LoggerFactory.getLogger(getClass());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doPost(req, resp);
	}

	@Override
	public void init() throws ServletException {
		super.init();
		Map<String, Map<String, String>> context = Config.getAllContextInfo();
		for (Entry<String, Map<String, String>> entry : context.entrySet()) {
			log.info("init application servlet key {} . value {} ", entry.getKey(), entry.getValue());
			this.getServletContext().setAttribute("config_" + entry.getKey(), entry.getValue());
		}
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}
}
